<?php
/**
 * @action 充值记录的控制器
 * @authors guo (guomingliangphp@163.com)
 * @date    2014-01-13 08:28:35
 * @version $Id$
 */

class OrderAction extends CommonAction {
 
    //指定代理商下的用户充值列表(总代理可以查看全部的充值记录)(包括对订单的查询)
    public function orderlist(){

        $names=R("Agent/userlist");
        $this->names=$names;

         $agentId=$_SESSION['agent']['id'];
         if ($agentId != 1){
            $condition["proxyid"] = $agentId;
         } 
//按指定的条件进行查询
        if(I("get.userName")){
            $userName=I("get.userName");
            $this->userName=$userName;
            $condition['username']=array("like","%$userName%");
            if(I("get.stime")){
                $stime=I("get.stime");
                $this->stime=$stime;
                if(I("get.etime")){
                  $etime=I("get.etime");
                  $this->etime=$etime;
                   $etime=date('Y-m-d',strtotime('+1 day',strtotime($etime)));
                    $condition['addtime']=array("between","$stime,$etime");
                }
            }
        }
        $condition["status"] = 1;
        $order = D("order");
        import("ORG.Util.Page");
        $totalnum = $order->where($condition)->count();
        $pagenumber = 10;

        $list = $order->where($condition)->page($_GET['p'])->limit($pagenumber)->order("id desc")->select();
        if(!$list){
            $this->error("无充值记录或该用户不存在","__URL__/orderlist");
        }
        $this->assign("list", $list);

        $page = new Page($totalnum, $pagenumber);
        $page->setConfig("prev", "上一页");
        $page->setConfig("next", "下一页");
        $page->setConfig("first", "首页");
        $page->setConfig("last", "末页");
        $page->setConfig("theme","<span class=pageinfo><u></u>当前%nowPage%/%totalPage%页</span>
                                     %first% %upPage%   %linkPage%  %downPage% %end%");
        $pageshow = $page->show();
        $this->assign("pageshow", $pageshow);
        $this->display();
    }
//创建订单(代理商给客户充值)
     public function orderaddsave($userId,$money)
    {
        $order = D("order");
        $agentId=$_SESSION['agent']['id'];
        $userModel=M("user");
        $agentModel = M("proxy");

        $agentData = $agentModel->find($agentId);
        $condition['ProxyId']=$agentId;
        $condition['Id']=$userId;
        $userData=$userModel->where($condition)->find();
        if(!$userData){ $this->error("该代理商不包含此用户");}
        $orderNumber = 'OD' . substr(date("YmdHis"), 2, 10) . mt_rand(100000, 999999);
        $order->ordernum = $orderNumber;
        $order->proxyid = $agentData['id'];
        $order->proxyname =$agentData['username'];
        $subject = "代理商为客户充值,客户名为:" . $userData['Username'];
        $order->subject = $subject;
        $order->pstart = $agentData['money'];
        $order->pend = $agentData['money'];
        $order->userid = $userId;
        $order->ordertype = 0; //代理商给客户充值（系统内的订单）
        $order->username = $userData['Username'];
        $order->money = $money; //充值金额
        $order->ustart = $userData['Usermoney']; //用户开始金额
        $order->uend =  $userData['Usermoney']+$money; //用户结束金额
        $order->status = 0; //默认是失败的，等到充值成功后再修改成为是成功的
        $order->ip = get_client_ip();
        $order->addtime = date('Y-m-d H:i:s');
        $order->isdel = 0; // 正常订单,非删除状态
        $orderid = $order->add();
        return $orderid;
    }

    //修改订单的状态(由失败--->成功)
    public function changestatus($orderId){

        $order=M("order");
        $result=$order->find($orderId);
        if(!$result){ $this->error("订单不存在");}
        $res=$order->where("id=$orderId")->setField("status",1);
        return $res;
    }

    //指定订单列表
    public function ordercontent()
    {
        $order =M("order");
        $orderId = I("get.orderId");
        $data = $order->find($orderId);
        $this->assign("data", $data);
        $this->display();
    }
}
?>